package dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import news.topic;
import toolDAO.Page;
import toolDAO.Tool;

public class TopicDAO implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -5932356572597734316L;
	public static final int PAGESIZE = 10;
	public static TopicDAO DAO = new TopicDAO();

	public static List<topic> showAll() {// ��ʾ���е�����
		List<topic> list = new ArrayList<topic>();
		String sql = "select * from topic";
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			con = Tool.open();
			st = con.prepareStatement(sql);
			rs = st.executeQuery();
			while (rs.next()) {
				topic t = new topic();
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				list.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		} finally {
			Tool.close(st, con, rs);
		}
		return list;
	}

	public static List<topic> show(int tid) {
		List<topic> list = new ArrayList<topic>();
		String sql = "select * from topic where tid=?";
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			con = Tool.open();
			st = con.prepareStatement(sql);
			st.setInt(1, tid);
			rs = st.executeQuery();
			while (rs.next()) {
				topic t = new topic();
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				list.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		} finally {
			Tool.close(st, con, rs);
		}
		return list;
	}

	public static List<topic> showIndex() {// �����Ƿ��ö�������
		List<topic> list = new ArrayList<topic>();
		String sql = "select * from topic where onTop=1 order by tid";
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		try {
			con = Tool.open();
			st = con.prepareStatement(sql);
			rs = st.executeQuery();
			while (rs.next()) {
				topic t = new topic();
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
				list.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		} finally {
			Tool.close(st, con, rs);
		}
		return list;
	}

	public int update(int tid, String tname) {// 鍒犻櫎鏂伴椈
		String sql = "update topic set tname=? where tid=? ";
		int k = Tool.executeUpdate2(sql, tname, tid);
		return k;
	}

	public static Page<topic> find(int pageNo) {// 分页
		if (pageNo < 1) {
			pageNo = 1;
		}
		String sql1 = "select count(1) from topic";
		String sql2 = "select * from topic limit ?,?";
		List<topic> l = new ArrayList<topic>();
		Connection con = Tool.open();
		java.sql.PreparedStatement ps = null;
		ResultSet rs = null;
		int total = 0;
		topic n = null;
		Page<topic> page = new Page<topic>();
		try {
			ps = con.prepareStatement(sql1);

			rs = ps.executeQuery();
			if (rs.next()) {
				total = rs.getInt(1);
			}
			Tool.close(ps, null, rs);
			ps = con.prepareStatement(sql2);
			ps.setInt(1, (pageNo - 1) * PAGESIZE);
			ps.setInt(2, PAGESIZE);
			rs = ps.executeQuery();
			while (rs.next()) {
				n = new topic();
				n.setTid(rs.getInt("tid"));
				n.setTname(rs.getString("tname"));
				l.add(n);
			}
			int pageTotals = total / PAGESIZE + (total % PAGESIZE == 0 ? 0 : 1);
			page.setPageNums(pageTotals);
			page.setList(l);
			page.setPageSize(PAGESIZE);
			page.setPageNo(pageNo);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		} finally {
			Tool.close(ps, null, rs);
		}
		return page;
	}

	public static topic showOne(int tid) {
		String sql = "select * from topic where tid=?";
		Connection con = null;
		PreparedStatement st = null;
		ResultSet rs = null;
		topic t = null;
		try {
			con = Tool.open();
			st = con.prepareStatement(sql);
			st.setInt(1, tid);
			rs = st.executeQuery();
			while (rs.next()) {
				t = new topic();
				t.setTid(rs.getInt("tid"));
				t.setTname(rs.getString("tname"));
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);

		} finally {
			Tool.close(st, con, rs);
		}
		return t;
	}

	public int del(int tid) {
		String sql = "delete from topic where tid=?";
		int k = Tool.executeUpdate2(sql, tid);
		return k;
	}

}
